/*
 * MainJFrame.java
 *
 * Created on 8.5.2011, 19:50:45
 */
package cz.muni.fi.pv168.confusinglibrary.app.gui;

import cz.muni.fi.pv168.confusinglibrary.database.DBUtils;
import cz.muni.fi.pv168.confusinglibrary.libs.Author;
import cz.muni.fi.pv168.confusinglibrary.libs.AuthorManagerDB;
import cz.muni.fi.pv168.confusinglibrary.libs.Book;
import cz.muni.fi.pv168.confusinglibrary.libs.BookManagerDB;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ListSelectionModel;
import javax.swing.SwingWorker;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.apache.commons.dbcp.BasicDataSource;

/**
 *
 * @author GNeps
 */
public class MainJFrame extends javax.swing.JFrame {

    private final AuthorManagerDB am = new AuthorManagerDB();
    private final BookManagerDB bm = new BookManagerDB();
    private volatile AuthorTableModel authorTableModel = null;
    private volatile BookTableModel bookTableModel = null;
    ResourceBundle language =
            java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/confusinglibrary/app/language/language");

    /** Creates new form MainJFrame */
    public MainJFrame() {
        setDatabaseConnection();
        initComponents();

        jTableOfAuthors.getTableHeader().setReorderingAllowed(false);
        authorTableModel = (AuthorTableModel) jTableOfAuthors.getModel();

        jTableBooks.getTableHeader().setReorderingAllowed(false);
        bookTableModel = (BookTableModel) jTableBooks.getModel();

        // Author table - Set up row selection listener
        ListSelectionModel authorRowSM = jTableOfAuthors.getSelectionModel();
        authorRowSM.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting()) {
                    return; // don't want to handle intermediate selections
                }
                ListSelectionModel rowSM = (ListSelectionModel) e.getSource();
                int selectedIndex = rowSM.getMinSelectionIndex();
                if (selectedIndex > -1) {
                    Long id = authorTableModel.getId(selectedIndex);
                    Author author = am.findAuthorById(id);
                    showAuthorInformation(author);
                } else {
                    jTextFieldAuthorsBorn.setText("");
                    jTextFieldAuthorsSurname.setText("");
                    jTextFieldAuthorsName.setText("");
                    jTextFieldAuthorsDied.setText("");
                }
            }
        });

        // Book table - Set up row selection listener
        ListSelectionModel bookRowSM = jTableBooks.getSelectionModel();
        bookRowSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting()) {
                    return; // don't want to handle intermediate selections
                }
                ListSelectionModel rowSM = (ListSelectionModel) e.getSource();
                int selectedIndex = rowSM.getMinSelectionIndex();
                if (selectedIndex > -1) {
                    Long id = bookTableModel.getId(selectedIndex);
                    Book book = bm.findBookById(id);
                    showBookInformation(book);
                } else {
                    showBookInformation(null);
                }
            }
        });

        BookDisplaySwingWorker bdsw = new BookDisplaySwingWorker();
        bdsw.execute();

        AuthorDisplaySwingWorker adsw = new AuthorDisplaySwingWorker();
        adsw.execute();
    }

    private void setDatabaseConnection() {
        Properties p = new Properties();
        InputStream in = null;
        try {
            in = getClass().getResourceAsStream("/cz/muni/fi/pv168/confusinglibrary/database/settings.properties");
            p.load(in);
            if (p.getProperty("database.host") != null && p.getProperty("database.username") != null && p.getProperty("database.password") != null) {
                BasicDataSource ds = new BasicDataSource();
                ds.setUrl(p.getProperty("database.host"));
                ds.setPassword(p.getProperty("database.password"));
                ds.setUsername(p.getProperty("database.username"));
                am.setDataSource(ds);
                bm.setDataSource(ds);
            } else {
                DBUtils.createSettingProperties();
                setDatabaseConnection();
            }
        } catch (IOException ex) {
            Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
                Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedTabbedPane = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jButtonAddBook = new javax.swing.JButton();
        jButtonUpdateBook = new javax.swing.JButton();
        jButtonRemoveBook = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabelName = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabelAuthor = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabelFiction = new javax.swing.JLabel();
        jLabelPublished = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextAreaDescription = new javax.swing.JTextArea();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableBooks = new javax.swing.JTable();
        jPanelAuthor = new javax.swing.JPanel();
        jButtonAddAuthor = new javax.swing.JButton();
        jButtonUpdateAuthor = new javax.swing.JButton();
        jButtonRemoveAuthor = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableOfAuthors = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldAuthorsName = new javax.swing.JTextField();
        jTextFieldAuthorsSurname = new javax.swing.JTextField();
        jTextFieldAuthorsBorn = new javax.swing.JTextField();
        jTextFieldAuthorsDied = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/confusinglibrary/app/language/language"); // NOI18N
        setTitle(bundle.getString("title_main")); // NOI18N

        jButtonAddBook.setText(bundle.getString("button_add_book"));
        jButtonAddBook.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddBookActionPerformed(evt);
            }
        });

        jButtonUpdateBook.setText(bundle.getString("button_update_book"));
        jButtonUpdateBook.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUpdateBookActionPerformed(evt);
            }
        });

        jButtonRemoveBook.setText(bundle.getString("button_remove_book"));
        jButtonRemoveBook.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveBookActionPerformed(evt);
            }
        });

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText(bundle.getString("title_book_information")); // NOI18N

        jLabel7.setText(bundle.getString("book_name")); // NOI18N

        jLabel8.setText(bundle.getString("book_author")); // NOI18N

        jLabel9.setText(bundle.getString("book_fiction")); // NOI18N

        jLabel10.setText(bundle.getString("book_published")); // NOI18N

        jLabel11.setText(bundle.getString("book_description")); // NOI18N

        jTextAreaDescription.setColumns(20);
        jTextAreaDescription.setEditable(false);
        jTextAreaDescription.setLineWrap(true);
        jTextAreaDescription.setRows(5);
        jTextAreaDescription.setWrapStyleWord(true);
        jScrollPane3.setViewportView(jTextAreaDescription);

        jTableBooks.setModel(new BookTableModel());
        jScrollPane1.setViewportView(jTableBooks);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel10)
                                        .addComponent(jLabel9)
                                        .addComponent(jLabel8)
                                        .addComponent(jLabel7))
                                    .addGap(18, 18, 18)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jLabelPublished, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabelFiction, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabelAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabelName, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel11))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButtonAddBook)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonUpdateBook)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonRemoveBook)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jLabelName, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(jLabelAuthor))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(jLabelFiction))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(jLabelPublished))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAddBook)
                    .addComponent(jButtonUpdateBook)
                    .addComponent(jButtonRemoveBook))
                .addContainerGap())
        );

        jTabbedTabbedPane.addTab(bundle.getString("MainJFrame.jPanel1.TabConstraints.tabTitle"), jPanel1); // NOI18N

        jButtonAddAuthor.setText(bundle.getString("button_add_author")); // NOI18N
        jButtonAddAuthor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddAuthorActionPerformed(evt);
            }
        });

        jButtonUpdateAuthor.setText(bundle.getString("button_update_author")); // NOI18N
        jButtonUpdateAuthor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUpdateAuthorActionPerformed(evt);
            }
        });

        jButtonRemoveAuthor.setText(bundle.getString("button_remove_author")); // NOI18N
        jButtonRemoveAuthor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveAuthorActionPerformed(evt);
            }
        });

        jTableOfAuthors.setModel(new AuthorTableModel());
        jTableOfAuthors.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(jTableOfAuthors);

        jLabel1.setText(bundle.getString("authors_name")); // NOI18N

        jLabel2.setText(bundle.getString("authors_surname")); // NOI18N

        jLabel3.setText(bundle.getString("author_born")); // NOI18N

        jLabel4.setText(bundle.getString("author_died")); // NOI18N

        jTextFieldAuthorsName.setEditable(false);

        jTextFieldAuthorsSurname.setEditable(false);

        jTextFieldAuthorsBorn.setEditable(false);

        jTextFieldAuthorsDied.setEditable(false);

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText(bundle.getString("title_author_information")); // NOI18N

        javax.swing.GroupLayout jPanelAuthorLayout = new javax.swing.GroupLayout(jPanelAuthor);
        jPanelAuthor.setLayout(jPanelAuthorLayout);
        jPanelAuthorLayout.setHorizontalGroup(
            jPanelAuthorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAuthorLayout.createSequentialGroup()
                .addGroup(jPanelAuthorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelAuthorLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButtonAddAuthor)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonUpdateAuthor)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonRemoveAuthor))
                    .addGroup(jPanelAuthorLayout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanelAuthorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelAuthorLayout.createSequentialGroup()
                                .addGroup(jPanelAuthorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanelAuthorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextFieldAuthorsName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)
                                    .addComponent(jTextFieldAuthorsSurname, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)
                                    .addComponent(jTextFieldAuthorsBorn, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)
                                    .addComponent(jTextFieldAuthorsDied, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)))
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanelAuthorLayout.setVerticalGroup(
            jPanelAuthorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelAuthorLayout.createSequentialGroup()
                .addGroup(jPanelAuthorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
                    .addGroup(jPanelAuthorLayout.createSequentialGroup()
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(jPanelAuthorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTextFieldAuthorsName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelAuthorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTextFieldAuthorsSurname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelAuthorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTextFieldAuthorsBorn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelAuthorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jTextFieldAuthorsDied, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelAuthorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAddAuthor)
                    .addComponent(jButtonUpdateAuthor)
                    .addComponent(jButtonRemoveAuthor))
                .addContainerGap())
        );

        jTabbedTabbedPane.addTab(bundle.getString("tab_author"), jPanelAuthor); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 825, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedTabbedPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE)
        );

        jTabbedTabbedPane.getAccessibleContext().setAccessibleName(bundle.getString("tab_book")); // NOI18N

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonAddAuthorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddAuthorActionPerformed
        setAuthorButtons(false);
        AuthorFormAddJFrame.startWindow(this);
    }//GEN-LAST:event_jButtonAddAuthorActionPerformed

    private void jButtonUpdateAuthorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUpdateAuthorActionPerformed
        int row = jTableOfAuthors.getSelectedRow();
        if (row != -1) {
            Long id = authorTableModel.getId(row);
            Author author = am.findAuthorById(id);
            setAuthorButtons(false);
            AuthorFormUpdateJFrame.startWindow(this, author, row);
        }
    }//GEN-LAST:event_jButtonUpdateAuthorActionPerformed

    private void jButtonRemoveAuthorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveAuthorActionPerformed
        int row = jTableOfAuthors.getSelectedRow();
        if (row != -1) {
            Logger.getLogger(MainJFrame.class.getName()).log(Level.FINER, "Removing author - app");
            Long id = authorTableModel.getId(row);
            Author author = am.findAuthorById(id);
            am.removeAuthor(author);
            jTableOfAuthors.clearSelection();//clearSelection();
            authorTableModel.removeAuthor(row);
        }
    }//GEN-LAST:event_jButtonRemoveAuthorActionPerformed

    private void jButtonAddBookActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddBookActionPerformed
        setBookButtons(false);
        BookFormJFrame.startWindow(this, am);
    }//GEN-LAST:event_jButtonAddBookActionPerformed

    private void jButtonUpdateBookActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUpdateBookActionPerformed
        int row = jTableBooks.getSelectedRow();
        if (row != -1) {
            Long id = bookTableModel.getId(row);
            Book book = bm.findBookById(id);
            setBookButtons(false);
            BookFormJFrame.startWindow(this, am, book, row);
        }
    }//GEN-LAST:event_jButtonUpdateBookActionPerformed

    private void jButtonRemoveBookActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveBookActionPerformed
        int row = jTableBooks.getSelectedRow();
        if (row != -1) {
            Logger.getLogger(MainJFrame.class.getName()).log(Level.FINER, "Removing book - app");
            Long id = bookTableModel.getId(row);
            Book book = bm.findBookById(id);
            bm.removeBook(book);
            jTableBooks.clearSelection();
            bookTableModel.removeAuthor(row);
        }
    }//GEN-LAST:event_jButtonRemoveBookActionPerformed

    protected void addAuthor(Author author) {
        Logger.getLogger(MainJFrame.class.getName()).log(Level.FINER, "Adding author");
        am.addAuthor(author);
        authorTableModel.addAuthor(author);
    }

    protected void updateAuthor(Author author, int row) {
        Logger.getLogger(MainJFrame.class.getName()).log(Level.FINER, "Updating author - app");
        am.updateAuthor(author);
        authorTableModel.updateAuthor(author, row);
        jTableOfAuthors.clearSelection();
        jTableOfAuthors.setRowSelectionInterval(row,row);
    }

    protected void addBook(Book book) {
        Logger.getLogger(MainJFrame.class.getName()).log(Level.FINER, "Adding book - app");
        bm.addBook(book);
        bookTableModel.addBook(book);
    }

    protected void updateBook(Book book, int row) {
        Logger.getLogger(MainJFrame.class.getName()).log(Level.FINER, "Updating book - app");
        bm.updateBook(book);
        bookTableModel.updateBook(book, row);
        jTableBooks.clearSelection();
        jTableBooks.setRowSelectionInterval(row,row);
    }

    private void showAuthorInformation(Author author) {
        jTextFieldAuthorsName.setText(author.getName());
        jTextFieldAuthorsSurname.setText(author.getSurname());

        SimpleDateFormat sdf = new SimpleDateFormat(language.getString("date_format_display"));
        Date date = author.getBorn();
        if (date == null) {
            jTextFieldAuthorsBorn.setText(language.getString("label_date_unknown"));
        } else {
            jTextFieldAuthorsBorn.setText(sdf.format(date));
        }

        date = author.getDied();
        if (date == null) {
            jTextFieldAuthorsDied.setText(language.getString("label_date_unknown"));
        } else {
            jTextFieldAuthorsDied.setText(sdf.format(date));
        }
    }

    private void showBookInformation(Book book) {
        if (book != null) {
            jLabelName.setText(book.getName());
            jLabelAuthor.setText(book.getAuthor().getSurname() + ", " + book.getAuthor().getName());
            jLabelFiction.setText(book.isFiction() ? language.getString("yes") : language.getString("no"));

            SimpleDateFormat sdf = new SimpleDateFormat(language.getString("date_format_display"));
            Date date = book.getPublished();
            if (date != null) {
                jLabelPublished.setText(sdf.format(date));
            } else {
                jLabelPublished.setText(language.getString("label_date_unknown"));
            }

            jTextAreaDescription.setText(book.getDescription() != null ? book.getDescription() : "");
        } else {
            jLabelName.setText("");
            jLabelAuthor.setText("");
            jLabelFiction.setText("");
            jLabelPublished.setText("");
            jTextAreaDescription.setText("");
        }
    }

    protected void setAuthorButtons(boolean enabled) {
        jButtonAddAuthor.setEnabled(enabled);
        jButtonRemoveAuthor.setEnabled(enabled);
        jButtonUpdateAuthor.setEnabled(enabled);
    }

    protected void setBookButtons(boolean enabled) {
        jButtonAddBook.setEnabled(enabled);
        jButtonUpdateBook.setEnabled(enabled);
        jButtonRemoveBook.setEnabled(enabled);
    }

    private class AuthorDisplaySwingWorker extends SwingWorker<Void, Void> {
        @Override
        protected Void doInBackground() throws Exception {
            Logger.getLogger(MainJFrame.class.getName()).log(Level.FINER, "Filling table of authors");
            List<Author> list = am.getAllAuthors();
            authorTableModel.fillTable(list);
            return null;
        }
    }

    private class BookDisplaySwingWorker extends SwingWorker<Void, Void> {
        @Override
        protected Void doInBackground() throws Exception {
            Logger.getLogger(MainJFrame.class.getName()).log(Level.FINER, "Filling table of books");
            List<Book> list = bm.getAllBooks();
            bookTableModel.fillTable(list);
            return null;
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAddAuthor;
    private javax.swing.JButton jButtonAddBook;
    private javax.swing.JButton jButtonRemoveAuthor;
    private javax.swing.JButton jButtonRemoveBook;
    private javax.swing.JButton jButtonUpdateAuthor;
    private javax.swing.JButton jButtonUpdateBook;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelAuthor;
    private javax.swing.JLabel jLabelFiction;
    private javax.swing.JLabel jLabelName;
    private javax.swing.JLabel jLabelPublished;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanelAuthor;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabbedTabbedPane;
    private javax.swing.JTable jTableBooks;
    private javax.swing.JTable jTableOfAuthors;
    private javax.swing.JTextArea jTextAreaDescription;
    private javax.swing.JTextField jTextFieldAuthorsBorn;
    private javax.swing.JTextField jTextFieldAuthorsDied;
    private javax.swing.JTextField jTextFieldAuthorsName;
    private javax.swing.JTextField jTextFieldAuthorsSurname;
    // End of variables declaration//GEN-END:variables
}
